package com.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Comment;
import com.entity.Contact;
import com.entity.UserProject;
import com.service.ContactService;
import com.util.StringHelp;
import com.util.StringSplitHelp;


public class ContactServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ContactService cs = new ContactService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");
		String hm=request.getMethod();
		
		HttpSession session=request.getSession();
		UserProject up=(UserProject) session.getAttribute("up");
		
		if(StringHelp.isEmpty(m)){
			list(request,response,up);
		}else if("add".equals(m)){
			if("GET".equals(hm)){
				toadd(request,response);
			}if("POST".equals(hm)){
				add(request,response,up);
			}
		}else if("show".equals(m)){
			show(request,response,up);
		}
	}


	private void show(HttpServletRequest request, HttpServletResponse response, UserProject up) throws IOException, ServletException {
		String id=request.getParameter("id");
		List<Contact> list = cs.findAllContact(up.getProjectid());	
		Contact contact=cs.findContactById(id);
		List<Comment> lis=cs.findAllCommentFromContact();
		request.setAttribute("con",list);
		request.setAttribute("cot", contact);
		request.setAttribute("comm", lis);
		request.setAttribute("len", id);
		request.getRequestDispatcher("/WEB-INF/pms/contact.jsp").forward(request, response);
	}
	private void add(HttpServletRequest request, HttpServletResponse response, UserProject up) throws IOException, ServletException {
		String name=request.getParameter("name");
		
		String company=request.getParameter("company");
		String phone=request.getParameter("phone");
		String tel=request.getParameter("tel");
		String email=request.getParameter("email");
		String address=request.getParameter("address");
		String home=request.getParameter("home");
		String weibo1=request.getParameter("weibo1");
		String weibo2=request.getParameter("weibo2");
		
		String adstruct=request.getParameter("adstruct");
		String projectid=up.getProjectid();
		request.setAttribute("up", up);
		if(name!=null ){
			if(StringHelp.isEmpty(weibo1) || StringHelp.isEmpty(weibo2)){
				String weibo=null;
				cs.saveContcat(name,company,phone,tel,email,address,home,weibo,adstruct,projectid);
			}
			response.sendRedirect("contact.jspx");
		}else{
			request.getRequestDispatcher("/WEB-INF/pms/addcontact.jsp?code=10001").forward(request, response);
		}
		
	}
	
	private void toadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/pms/addcontact.jsp").forward(request, response);
	}
	
	private void list(HttpServletRequest request, HttpServletResponse response, UserProject up) throws IOException, ServletException {
		List<Contact> list = cs.findAllContact(up.getProjectid());	
		Contact cot=list.get(0);
		String len=null;
		List<Comment> list2=cs.findAllCommentFromContact();
		request.setAttribute("con",list);
		request.setAttribute("cot", cot);
		request.setAttribute("len", len);
		request.setAttribute("comm", list2);
		request.setAttribute("up",up);
		request.getRequestDispatcher("/WEB-INF/pms/contact.jsp").forward(request, response);
		
	}	
}
